package com.example.scancodelogindemo.redisqueue;

import com.example.scancodelogindemo.utils.RedisGenerateKeyUtils;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class PublisherService {

    private final RedissonClient redissonClient;

    @Autowired
    public PublisherService(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    public <T> void publishMessage(String channel, T message) {
        final String topic = RedisGenerateKeyUtils.buildKey(channel);
        final RTopic changeTopic = redissonClient.getTopic(topic, JsonJacksonCodec.INSTANCE);
        changeTopic.publish(message);
        log.info("PublisherService#publishMessage channel: {} 收到消息 {}.", topic, message);
    }

}
